﻿using Shop.BLL;
using Shop.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Shop.MVC.Areas.Admin.Controllers
{
    public class ProductController : Controller
    {
        BaseManager<Category> cateManager = new BaseManager<Category>();
        BaseManager<Product> productManager = new BaseManager<Product>();
        InfoManager infoManager=new InfoManager();
        ColorManager colorManager = new ColorManager();
        BaseManager<Pictrue> picManager=new BaseManager<Pictrue>();

        //列表页面
        public ActionResult Index()
        {
            var list = cateManager.GetList();
            list.Insert(0, new Category() { Id = -1, Title = "请选择" });
            ViewBag.Cate = new SelectList(list, "Id", "Title");
            return View();
        }

        public ActionResult GetList(string name="",int cateid=-1,int pageIndex=1,int pageSize=5)
        {
            int total;
            
            string where = "name like @name";
            List<SqlParameter> sp = new List<SqlParameter>()
            {
                new SqlParameter("@name","%"+name+"%")
            };
            if (cateid > 0)
            {
                where += " and [CategrayId]=@cateid";
                sp.Add(new SqlParameter("@cateid", cateid));
            }
            var list = productManager.GetList(where, sp.ToArray(), pageIndex, pageSize, out total);
            return Json(new { total=total, rows = list },JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(int id)
        {
            productManager.Delete(id);
            return Json(new { success = true, msg = "删除成功！" },JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var list = cateManager.GetList();
            ViewBag.Cate = new SelectList(list, "Id", "Title");

            var model = new Product();

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]  // 不验证输入（如果有危险则放行）
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                if (product.Id == 0)
                {
                    productManager.Add(product);
                    return Json(new { success = true, msg = "添加成功！" });
                }
                productManager.Update(product);
                return Json(new { success = true, msg = "修改成功！" });
            }
            return Json(new { success = false, msg = "验证错误！" });
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var list = cateManager.GetList();
            ViewBag.Cate = new SelectList(list, "Id", "Title");
            var entity=productManager.GetEntity(id);
            return View("create", entity);
        }


        /// <summary>
        /// 设置规格
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SetInfo(int id)
        {
            // 获取商品信息
            var product = productManager.GetEntity(id);
            ViewBag.Product = product;
            // 获取已有的规格列表
            var list = infoManager.GetListByPId(id);

            return View(list);
        }

        /// <summary>
        /// 添加规格
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult CreateInfo(int productId)
        {
            var model =new ProductInfo() { ProductId = productId };
            return View(model);
        }

        [HttpPost]
        public ActionResult CreateInfo(ProductInfo model)
        {
            if (ModelState.IsValid)
            {
                if (model.Id == 0)
                {
                    infoManager.Add(model);
                }
                else
                {
                    infoManager.Update(model);
                }
                return Json(new { success = true, msg = "添加成功" });
            }
            return Json(new { success = false, msg = "验证失败！" });
        }

        //设置颜色
        [HttpGet]
        public ActionResult SetColor(int id)
        {
            // 查询商品所有颜色
            ViewBag.Colors=colorManager.GetColors(id);
            ViewBag.InfoId = id;
            return View();
        }

        [HttpPost]
        public ActionResult CreateColor(Color model)
        {
            if (ModelState.IsValid)
            {
                colorManager.Add(model);
                return RedirectToAction("SetColor",new {Id=model.ProductInfoId});
            }
            return View("SetColor");
        }
        // 设置颜色图片
        public ActionResult CreateImage(Pictrue model)
        {
            picManager.Add(model);
            return Json(new { success = true });
        }
    }
}